
//export default { Vector } 

var Vector = /** @class */ (function () {
    //构造函数
    function Vector() {
        var arg = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            arg[_i] = arguments[_i];
        }
        if (arg.length == 1) {
            this.x = arg[0].x;
            this.y = arg[0].y;
        }
        else if (arg.length == 2) {
            if (typeof (arg[0]) == "number") {
                // alert()
                this.x = arg[0];
                this.y = arg[1];
            }
            else {
                this.x = arg[1].x - arg[0].x;
                this.y = arg[1].y - arg[0].y;
            }
        }
        else {
            throw console.error("参数不正确1个或者2个");
        }
        this.angle = Math.atan2(this.y, this.x) * 180 / Math.PI;
        this.length = Math.sqrt(this.x * this.x + this.y * this.y);
    }
    //角度的设置和获取
    Vector.getangle = function (v1, v2) {
        var x = v2.x - v1.x;
        var y = v2.y - v1.y;
        var _angle = Math.atan2(y, x) * 180 / Math.PI;
        return _angle;
    };
    //向量设置角度
    Vector.prototype.setangle = function (a) {
        this.x = this.length * Math.cos(a * Math.PI / 180);
        this.y = this.length * Math.sin(a * Math.PI / 180);
        this.angle = a;
    };
    //大小的设置和获取
    Vector.prototype.getlength = function (v1, v2) {
        var x = v2.x - v1.x;
        var y = v2.y - v1.y;
        //  let _angle = Math.atan2(y, x) * 180 / Math.PI;
        var _length = Math.sqrt(x * x + y * y);
        return _length;
    };
    Vector.prototype.setlength = function (l) {
        var ratio = l / this.length;
        this.length = l;
        this.scale(ratio);
    };
    Vector.prototype.scale = function (s) {
        this.x *= s;
        this.y *= s;
    };
    //以字符串的形式返回向量类的2个属性
    // public toString(): String {
    //     var tx: String = this.x.toFixed(2);
    //     var ty: String = this.y.toFixed(2);
    //     return "[" + tx + "," + ty + "]";
    // }
    Vector.prototype.toString = function () {
        var tx = this.x.toFixed(2);
        var ty = this.y.toFixed(2);
        var tangle = this.angle.toFixed(2);
        var tlength = this.length.toFixed(2);
        return "[x=" + tx + ",y=" + ty + ",angle=" + tangle + ",length=" + tlength + "]";
    };
    Vector.toString2 = function (v) {
        var tx = v.x.toFixed(2);
        var ty = v.y.toFixed(2);
        var angle = Math.atan2(v.y, v.x) * 180 / Math.PI;
        var length = Math.sqrt(v.x * v.x + v.y * v.y);
        var tangle = angle.toFixed(2);
        var tlength = length.toFixed(2);
        return "[x=" + tx + ",y=" + ty + ",angle=" + tangle + ",length=" + tlength + "]";
    };
    //把向量的xy属性赋值给对象
    Vector.prototype.display = function (obj) {
        obj.x = this.x;
        obj.y = this.y;
    };
    //重新初始化
    Vector.prototype.reset = function (x, y) {
        this.x = x;
        this.y = y;
    };
    //得到一个复制的向量
    Vector.prototype.clone = function () {
        return new Vector(this.x, this.y);
    };
    //两个向量相加
    Vector.prototype.plus = function (v) {
        this.x += v.x;
        this.y += v.y;
    };
    //两个向量相加的结果返回给另一个新的向量
    Vector.prototype.plusNew = function (v) {
        return new Vector(this.x + v.x, this.y + v.y);
    };
    //两个向量相减
    Vector.prototype.minus = function (v) {
        this.x -= v.x;
        this.y -= v.y;
    };
    Vector.prototype.minusNew = function (v) {
        return new Vector(this.x - v.x, this.y - v.y);
    };
    //比较两个对象是否相等
    Vector.prototype.equals = function (v) {
        return (this.x == v.x && this.y == v.y);
    };
    //标准化向量
    Vector.prototype.normal = function () {
        var len = this.length;
        return new Vector(this.x / len, this.y / len);
    };
    //判定向量是否为单位向量
    Vector.prototype.isNormalized = function () {
        return length == 1.0;
    };
    //点积
    Vector.prototype.dot = function (v) {
        return this.x * v.x + this.y * v.y;
    };
    //叉乘(即向量的矢量积)
    Vector.prototype.crossProd = function (v) {
        return this.x * v.y - this.y * v.x;
    };
    //当前向量在向量v上的水平投影
    Vector.prototype.parVector = function (v) {
        var len = v.length;
        return v.scaleNew((this.x * v.x + this.y * v.y) / (len * len));
    };
    //当前向量在向量v上的垂直投影
    Vector.prototype.perVector = function (v) {
        return this.minusNew(this.parVector(v));
    };
    //2向量的距离
    Vector.prototype.distance = function (v) {
        var tx = v.x - this.x;
        var ty = v.y - this.y;
        return Math.sqrt(tx * tx + ty * ty);
    };
    //2向量的距离
    Vector.distance = function (v1, v2) {
        var tx = v2.x - v1.x;
        var ty = v2.y - v1.y;
        return Math.sqrt(tx * tx + ty * ty);
    };
    //----------------------------------------
    //向量求逆，即旋转180度
    Vector.prototype.negate = function () {
        this.x = -this.x;
        this.y = -this.y;
    };
    Vector.prototype.negateNew = function () {
        return new Vector(-this.x, -this.y);
    };
    //向量缩放
    // public scale(s): void {
    //     this.x *= s;
    //     this.y *= s;
    // }
    Vector.prototype.scaleNew = function (s) {
        return new Vector(this.x * s, this.y * s);
    };
    //法向量，返回与当前向量垂直的向量
    Vector.prototype.getNormal = function () {
        return new Vector(-this.y, this.x);
    };
    Vector.prototype.isPerpTo = function (v) {
        return this.dot(v) == 0;
    };
    //向量的夹角
    Vector.prototype.angleBetween = function (v) {
        var dp = this.dot(v);
        var cosAngle = dp / (this.length * v.length);
        var radian = Math.acos(cosAngle);
        return radian * 180 / Math.PI;
    };
    //返回二个向量之间的夹角
    Vector.angleBetween = function (v1, v2) {
        if (!v1.isNormalized()) {
            v1 = v1.clone().normal();
        }
        if (!v2.isNormalized()) {
            v2 = v2.clone().normal();
        }
        return Math.acos(v1.dot(v2)); //建议先回顾一下http://www.cnblogs.com/yjmyzz/archive/2010/06/06/1752674.html中提到的到夹角公式
    };
    //判定给定的向量是否在本向量的左侧或右侧，左侧返回-1，右侧返回1
    Vector.prototype.sign = function (v2) {
        return this.perp().dot(v2) < 0 ? -1 : 1;
    };
    //返回与本向量垂直的向量(即自身顺时针旋转90度，得到一个新向量)
    Vector.prototype.perp = function () {
        return new Vector(-this.y, this.x); //建议回顾一下"坐标旋转"
    };
    return Vector;
}());
//console.log(Vector)

window.Vector = Vector